package com.tuanzi.loan.business.repository.collaborate.impl;

import com.google.common.collect.Lists;
import com.tuanzi.loan.business.repository.collaborate.custom.StatCollaborateMarketLoginRepositoryCustom;
import com.tuanzi.loan.core.repository.JOOQBaseRepositoryPlus;
import com.tuanzi.loan.vo.request.statistics.login.StatMarketLoginRequest;
import com.tuanzi.loan.vo.response.collaborate.StatCollaborateMarketLoginResponse;
import com.tuanzi.server.loan_base.util.DateUtils;
import org.apache.commons.lang3.StringUtils;
import org.jooq.Condition;
import org.jooq.Record;
import org.jooq.SelectSeekStep1;
import org.jooq.impl.DSL;
import org.jooq.impl.TableImpl;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Repository;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

import static com.tuanzi.loan.generated.jooq.stat.Tables.STAT_COLLABORATE_MARKET_LOGIN;
import static com.tuanzi.loan.generated.jooq.stat.Tables.STAT_MARKET_LOGIN;

/**
 * User: liangZongXi
 * Create time: 13:56 2019/5/9
 * Description:
 */
@Repository
public class StatCollaborateMarketLoginRepositoryImpl  extends JOOQBaseRepositoryPlus implements StatCollaborateMarketLoginRepositoryCustom {

    @Override
    public Page<StatCollaborateMarketLoginResponse> list(StatMarketLoginRequest searchRequest, Pageable pageable, List<Integer> channels) {
        if(null == channels || channels.size() == 0){
            return new PageImpl<>(Lists.newArrayList());
        }

        Condition timeOneCondition = DSL.trueCondition();
        Condition timeTwoCondition = DSL.trueCondition();
        Date endDate = searchRequest.getEndDate();
        Date startDate = searchRequest.getStartDate();
        if(null != startDate){
            java.sql.Date date = new java.sql.Date(startDate.getTime());
            timeOneCondition = timeOneCondition.and(STAT_COLLABORATE_MARKET_LOGIN.CREATE_DATE.ge(date));
            timeTwoCondition = timeTwoCondition.and(STAT_MARKET_LOGIN.CREATE_DATE.ge(date));

        }
        if(null != endDate){
            java.sql.Date endSqlDate = new java.sql.Date(endDate.getTime());
            timeOneCondition = timeOneCondition.and(STAT_COLLABORATE_MARKET_LOGIN.CREATE_DATE.le(endSqlDate));
            timeTwoCondition = timeTwoCondition.and(STAT_MARKET_LOGIN.CREATE_DATE.le(endSqlDate));
        }
        Condition channelOneCondition = DSL.trueCondition();
        Condition channelSecondCondition = DSL.trueCondition();
        String channelCode = searchRequest.getChannelCode();
        if(StringUtils.isNotBlank(channelCode)){
            channelOneCondition = channelOneCondition.and(STAT_COLLABORATE_MARKET_LOGIN.CHANNEL_CODE.eq(channelCode));
            channelSecondCondition = channelSecondCondition.and(STAT_MARKET_LOGIN.CHANNEL_CODE.eq(channelCode));
        }

        long todayTimestamp = Timestamp.valueOf(DateUtils.getCurrentDate() +" 00:00:00").getTime();
        SelectSeekStep1<Record, java.sql.Date> records = dslContext.select(STAT_COLLABORATE_MARKET_LOGIN.fields()).from(STAT_COLLABORATE_MARKET_LOGIN)
                .where(STAT_COLLABORATE_MARKET_LOGIN.CHANNEL_CODE.in(channels))
                .and(timeOneCondition).and(channelOneCondition)

                .unionAll(dslContext.select(STAT_MARKET_LOGIN.fields()).from(STAT_MARKET_LOGIN)
                        .where(STAT_MARKET_LOGIN.CHANNEL_CODE.in(channels))
                        .and(STAT_MARKET_LOGIN.CREATE_DATE.eq(new java.sql.Date(todayTimestamp)))
                        .and(timeTwoCondition)
                        .and(channelSecondCondition)).orderBy(STAT_COLLABORATE_MARKET_LOGIN.CREATE_DATE.desc());

        int count = dslContext.fetchCount(records);
        List<StatCollaborateMarketLoginResponse> into = records.fetch().into(StatCollaborateMarketLoginResponse.class);

        return new PageImpl<>(into, pageable, count);
    }

    @Override
    protected TableImpl getTable() {
        return STAT_COLLABORATE_MARKET_LOGIN;
    }
}
